package com.leetcode.ask;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Number560 {
    public static void main(String[] args) {
        int [] nums = new int[]{1,2,1,2,1};
        int t = subarraySum(nums,3);
        System.out.println(t);
    }
    public static int subarraySum(int[] nums, int k) {
        int presum = 0;
        int count = 0;
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        for (int num : nums) {
            presum += num;
            int t = presum - k;
            if (map.containsKey(t)) {
                count += map.get(t);
            }
            map.put(presum, map.getOrDefault(presum, 0) + 1);

        }
        return count;
    }
}
